import { ref, onMounted } from 'vue';
import Chart from 'chart.js/auto';

export default {
    name: 'DriveDetail',
    props: {
        id: {
            type: Number,
            default: 1,
        },
    },
    setup(props) {
        const students = ref(0);
        const coaches = ref(0);
        const cars = ref(0);
        const income = ref(0);
        const expense = ref(0);
        const driveChart = ref(null);
        const incomeExpensePieChart = ref(null);

        const getDetail = async (id) => {
            const response = await fetch(`http://localhost:8081/admin/detail/${id}`);
            const result = await response.json();
            return result.data;
        };

        const drawBarChart = async () => {
            const detail = await getDetail(props.id);
            students.value = detail.stu_num;
            coaches.value = detail.co_num;
            cars.value=detail.car_num;
            income.value = detail.income;
            expense.value = detail.outcome;
            console.log(income.value,expense.value);

            const chartData = [
                { type: "未通过", count: detail.noPass },
                { type: "科目一", count: detail.passKm1 },
                { type: "科目二", count: detail.passKm2 },
                { type: "科目三", count: detail.passKm3 },
                { type: "科目四", count: detail.passKm4 },
            ];

            const chartConfig = {
                type: 'bar',
                data: {
                    labels: chartData.map(item => item.type),
                    datasets: [{
                        label: '学员统计',
                        data: chartData.map(item => item.count),
                        backgroundColor: [
                            'rgba(255, 99, 132, 0.6)',
                            'rgba(75, 192, 192, 0.6)',
                            'rgba(255, 205, 86, 0.6)',
                            'rgba(54, 162, 235, 0.6)',
                            'rgba(153, 102, 255, 0.6)',
                        ],
                        borderColor: [
                            'rgba(255, 99, 132, 1)',
                            'rgba(75, 192, 192, 1)',
                            'rgba(255, 205, 86, 1)',
                            'rgba(54, 162, 235, 1)',
                            'rgba(153, 102, 255, 1)',
                        ],
                        borderWidth: 2,
                        borderRadius: 5,
                    }],
                },
                options: {
                    plugins: {
                        title: {
                            display: true,
                            text: '驾校学员通过状况',
                            font: {
                                size: 24,
                                weight: 'bold'
                            }
                        },
                        legend: {
                            display: true,
                            position: 'top',
                        },
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            grid: {
                                borderWidth: 1,
                                color: 'rgba(100, 100, 100, 0.2)',
                            },
                            ticks: {
                                color: 'rgba(100, 100, 100, 0.8)',
                            },
                        },
                        x: {
                            grid: {
                                display: false,
                            },
                            ticks: {
                                color: 'rgba(100, 100, 100, 0.8)',
                            },
                        },
                    },
                },
            };

            if (driveChart.value) {
                const ctx = driveChart.value.getContext('2d');
                new Chart(ctx, chartConfig);
            }
        };
        //饼状图
        const drawPieChart = () => {
            const pieChartData = [
                { type: "收入", count: income.value },
                { type: "支出", count: expense.value }
            ];
            console.log("Pie Chart Data:", pieChartData); // 新增这行
            const pieChartConfig = {
                type: 'pie',
                data: {
                    labels: pieChartData.map(item => item.type),
                    datasets: [{
                        data: pieChartData.map(item => item.count),
                        backgroundColor: [
                            'rgba(75, 192, 192, 0.6)',
                            'rgba(255, 99, 132, 0.6)',
                        ],
                        borderColor: [
                            'rgba(75, 192, 192, 1)',
                            'rgba(255, 99, 132, 1)',
                        ],
                        borderWidth: 1
                    }]
                },
                options: {
                    plugins: {
                        title: {
                            display: true,
                            text: '驾校收入支出',
                            font: {
                                size: 24,
                                weight: 'bold'
                            }
                        },
                        legend: {
                            display: true,
                            position: 'top'
                        }
                    }
                }
            };

            if (incomeExpensePieChart.value) {
                const pieCtx = incomeExpensePieChart.value.getContext('2d');
                new Chart(pieCtx, pieChartConfig);
            }
        };

        onMounted(async () => {
            await drawBarChart();
            drawPieChart();
        });

        return { students, coaches, cars, driveChart, incomeExpensePieChart, drawBarChart, drawPieChart };
    },
};